package org.javastudy.tang.lambda.example2;

import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * <p><em>Created on 2022/5/9 5:34 下午</em></p>
 *
 * @author fuzhongtang
 * @since 1.0.1
 */
public class TestNewStudentByJdk7 {
    public static void main(String[] args) {
        List<Student> stuList = Arrays.asList(
                new Student("张三","001",19),
                new Student("李四","005",22),
                new Student("王五","010",14),
                new Student("赵六","004",18),
                new Student("何琦","006",12)
        );
        System.out.println("sortByNo-------------");
        sortByNo(stuList);
        printlistAll(stuList);

        System.out.println("filterCondition--找出成年人名单-----------");
        filterCondition(stuList, new Condition<Student>() {
            @Override
            public boolean compare(Student t) {
                //找出成年人
                if(t.getAge()>=18) {
                    return true;
                }
                return false;
            }
        });
    }

    //根据学号排序
    public static void sortByNo(List<Student> list) {
        Collections.sort(list,new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                return o1.getNo().compareTo(o2.getNo());
            };
        });
    }
    //列表所有学生信息
    public static void printlistAll(List<Student> stuList) {
        for(Student stu:stuList) {
            System.out.println(stu.getNo()+" "+stu.getName());
        }

    }

    public static void filterCondition(List<Student> stuList,Condition<Student> c) {
        for(Student stu:stuList) {
            if(c.compare(stu)) {
                System.out.println("true "+stu.getNo()+" "+stu.getName()+" "+stu.getAge());
            }
            else
            {
                System.out.println("false "+stu.getNo()+" "+stu.getName()+" "+stu.getAge());
            }
        }
    }

    interface Condition<T> {
        public boolean compare(T t);
    }
}
